<?php


class dbasupplier{

   function dbasupplier (&$dba){
     $this->dba = $dba;
   }
  
  
   // Lieferer
  
   function getSuppliers ($ordered = true){
     if ($ordered){
       $orderStr = ' order by name';
     }
     $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopSupplier$orderStr;");
     return $this->dba->fetch_rows_array();
   }

   function getSuppliersByArea ($home ='',$abroad='',$ordered = true){
     if ($home || $abroad) {
       $where .= ' where ';
       if ($home == 'Y') {
         $where .= "home ='$home' ";
         if ($abroad) $where .= 'AND ';
       }
       if ($abroad == 'Y') {
         $where .= "abroad = '$abroad' ";
       }
     }
     if ($ordered){
       $orderStr = ' order by name';
     }
     $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopSupplier$where $orderStr;");
     return $this->dba->fetch_rows_array();
   }

   function getSupplier (&$result,$id){
     $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopSupplier where id = $id;");
     $result = $this->dba->fetch_row_array();
     return $check;
   }

   function getSupplierSF ($id){
     $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopSupplier where id = $id;");
     if (is_object($sth))
       return $sth->fetch_row_array();
   }

   function delSupplier ($id){
     $this->dba->query("delete from ".$this->dba->tableforwarder."_shopSupplierProgRate where supplier_id = $id;");
     return $this->dba->query("delete from ".$this->dba->tableforwarder."_shopSupplier where id = $id;");
   }

   function updateSupplier ($id,$name,$tax,$home,$abroad,$postageCalc){
     if (!$home)   $home   = 'N';
     if (!$abroad) $abroad = 'N';
     return $this->dba->query("update ".$this->dba->tableforwarder."_shopSupplier set name='$name',tax='$tax', home='$home', abroad='$abroad', postageCalc='$postageCalc' where id = $id;");
   }

   function addSupplier ($name,$tax,$home,$abroad,$postageCalc){
     if (!$home)   $home   = 'N';
     if (!$abroad) $abroad = 'N';
     return $this->dba->query("insert into ".$this->dba->tableforwarder."_shopSupplier (name,tax,home,abroad, postageCalc) values ('$name','$tax','$home','$abroad', '$postageCalc');");
   }


   // Staffelungen

   function getSupplierProgRates (&$data, $s_id,$ordered = true){
     if ($ordered){
       $orderStr = ' order by cost desc';
     }
     $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopSupplierProgRate where supplier_id = '$s_id'$orderStr;");
     $data  = $this->dba->fetch_rows_array();
     return $check;
   }

   function getPostage ($s_id){
     $this->getSupplierProgRates ($data,$s_id,true);
     return $data;
   }
   
   function getSupplierProgRate (&$result,$id){
     $check = $this->dba->query("select * from ".$this->dba->tableforwarder."_shopSupplierProgRate where id = $id;");
     $result = $this->dba->fetch_row_array();
     return $check;
   }

   function delSupplierProgRate ($id){
     return $this->dba->query("delete from ".$this->dba->tableforwarder."_shopSupplierProgRate where id = $id;");
   }

   function updateSupplierProgRate ($id,$kg,$amount,$cost){
     return $this->dba->query("update ".$this->dba->tableforwarder."_shopSupplierProgRate set kg='$kg', amount='$amount', cost='$cost' where id = $id;");
   }
   
   function addSupplierProgRate ($s_id,$kg,$amount,$cost){
     return $this->dba->query("insert into ".$this->dba->tableforwarder."_shopSupplierProgRate (supplier_id,kg,amount,cost) values ('$s_id','$kg','$amount','$cost');");
   }


}
?>
